package com.ithinks.flow.common.utils.middleware.rabbitMQ.pubsubmode;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description
 * @ClassName PubSub
 * @Author MR.LI
 * @date 2020.11.25 19:31
 */
public class Producer {

    private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";

    private static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    private static final String EXCHANGE_FANOUT_INFORM = "exchange_fanout_inform";

    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //队列名称
    private static String USERNAME = "guest";

    private static String PASSWORD = "guest";

    public static void main(String[] args)  {
        Connection connection = ConnectionUtils.getConnection(IP,PORT,VHOST,USERNAME,PASSWORD);

        Channel channel = null;
        try {
            channel = connection.createChannel();
            //声明交换机 String exchange, BuiltinExchangeType type
            /**
             * 参数明细
             * 1、交换机名称
             * 2、交换机类型，fanout、topic、direct、headers
             */
            channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM, BuiltinExchangeType.FANOUT);
            //声明队列:队列名称，是否持久化，是否独占此队列，队列不用是否自动删除，参数
            channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);

            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
            //交换机与队列绑定：参数队列名称， 交换机名称，路由key
            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_FANOUT_INFORM,"");
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_FANOUT_INFORM,"");

            for(int i=0;i<50;i++)
            {
                String message = "inform to use "+i;
                channel.basicPublish(EXCHANGE_FANOUT_INFORM,"",null,message.getBytes());
                System.out.println("senf Message is ：" + message );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    /**
     * publish/subscribe与worker区别
     * 1、work queues 不用定义交换机，而publish/subscribe需要交换机
     * 2、pub/sub是面向交换机发消息，workqueue面向队列发消息（底层使用默认交换机）
     * 3、pub/sub需要设置队列和交换机绑定，workqueue不需要，实际work queue会将队列绑定到默认交换机
     *
     * 建议使用pub/sub模式，发布订阅比工作队列模式更强大，发布订阅模式可指定自己专用的交换机
     */
}
